package com.zhuiyun.project.api.article.service.impl;

import com.zhuiyun.project.api.article.entity.Article;
import com.zhuiyun.project.api.article.mapper.PublishArticleMapper;
import com.zhuiyun.project.api.article.model.ArticleModel;
import com.zhuiyun.project.api.article.service.PublishArticleService;
import com.zhuiyun.project.api.filemanage.entity.FileManage;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.ObjectUtils;

import com.zhuiyun.project.util.UploadFileUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import static cn.hutool.core.date.DateTime.now;

/**
 * @ClassName PublishArticleServiceImpl
 * @Description TODO
 * @Author maomao
 * @Date 2023/6/12 15:19
 **/

@Service
public class PublishArticleServiceImpl implements PublishArticleService {

    @Autowired
    PublishArticleMapper publishArticleMapper;


    @Override
        public String savePublishArticle(MultipartFile file,  FileManage fileManage) throws CommonException {
            if (file != null) {
                String s=file.getOriginalFilename();
                String type=s.substring(s.lastIndexOf("."),s.length());
                //如果上传类型为.mp4格式，则返回id时在后面加+作为区分
                //如果不是则正常返回图片id
                if(type.equals(".mp4")) {
                    String fileId = UploadFileUtils.uploadAvatar(file);
                    String i = fileId+"+";
                    return i;
                }else {
                    String fileId = UploadFileUtils.uploadAvatar(file);
                    return fileId;
                }
            }else {
                return null;
            }

    }
    @Override
    public CommonResult savePublishArticle1(MultipartFile file,  FileManage fileManage) throws CommonException {
        if (file != null) {
            String s=file.getOriginalFilename();
            String type=s.substring(s.lastIndexOf("."),s.length());
            //如果上传类型为.mp4格式，则返回id时在后面加+作为区分
            //如果不是则正常返回图片id
            if(type.equals(".mp4")) {
                String fileId = UploadFileUtils.uploadAvatar(file);
                String i = fileId+"+";
                return CommonResult.ok(i);
            }else {
                String fileId = UploadFileUtils.uploadAvatar(file);
                return CommonResult.ok(fileId);
            }
        }else {
            return null;
        }

    }

    @Override
    public CommonResult insertPublishArticle(Article article) {
        if (article!=null){
            Integer userId = article.getUserId();
            int i1 = publishArticleMapper.selectDoctorId(userId);
            article.setUserId(i1);
            article.setIsPublished(2);
            article.setApprovalStatus(0);
            article.setCreateTime(now());
            article.setPublishTime(now());
            article.setUpdateTime(now());
            String images = article.getImages();
            if(images!=null){
                //去除字符串的最后一个“，”
                String substring = images.substring(0,images.length() - 1);
                //将字符串通过“，”分割
                String[] split = substring.split(",");
                //创建两个新字符串
                StringBuilder withPlusBuilder = new StringBuilder();
                StringBuilder withoutPlusBuilder = new StringBuilder();
                //遍历数组
                for (int i = 0; i < split.length; i++) {
                    String sub = split[i];

                    if (sub.endsWith("+")) {
                        if (split.length > 1) {
                            withPlusBuilder.append(sub, 0, sub.length() - 1).append(",");
                        } else {
                            withPlusBuilder.append(sub, 0, sub.length() - 1);
                        }
                    } else {
                        if (split.length > 1) {
                            withoutPlusBuilder.append(sub).append(",");
                        } else {
                            withoutPlusBuilder.append(sub);
                        }
                    }
                }
                String withPlus = withPlusBuilder.toString();
                String withoutPlus = withoutPlusBuilder.toString();

                if (withPlus.length() > 0) {
                    if (withPlus.contains(",")) {
                        withPlus = withPlus.substring(0, withPlus.length() - 1);
                        article.setVideo(withPlus);
                        article.setImages(withoutPlus);
                    }else {
                        article.setVideo(withPlus);
                        article.setImages(withoutPlus);
                    }
                }

                if (withoutPlus.length() > 0) {
                    if (withoutPlus.contains(",")) {
                        withoutPlus = withoutPlus.substring(0, withoutPlus.length() - 1);
                        article.setVideo(withPlus);
                        article.setImages(withoutPlus);
                    }else {
                        article.setVideo(withPlus);
                        article.setImages(withoutPlus);
                    }
                }
            }
            //添加
            publishArticleMapper.insertSelective(article);
            return CommonResult.ok(200);
        }
        return CommonResult.ok(6666);
    }
}
